package cn.itsource.basic.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.FastDfsApiOpr;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    @RequestMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        //获取文件名字
        String originalFilename = file.getOriginalFilename();
        //获取文件的后缀名
        String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        /*
            file：byte[] 数组
            extName:文件的后缀
         */
        try {
            String   upload = FastDfsApiOpr.upload(file.getBytes(), extName);
            return  AjaxResult.me().setResultObj(upload);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }


}
